package org.xiaom.butler.service;

import java.util.ArrayList;
import java.util.List;

import org.xiaom.butler.bean.Content;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ContentService extends BeanDataService<Content> {

	public ContentService(Context context) {
		super(context);
	}

	@Override
	public void insert(List<Content> beans) {

		for (Content bean : beans) {
			ContentValues cv = splitBean(bean);
			this.db.insert(Content.table_name, null, cv);
		}
	}

	@Override
	public void update(List<Content> beans) {
		for (Content bean : beans) {
			ContentValues cv = splitBean(bean);

			this.db.update(Content.table_name, cv, Content.id_ + "="
					+ bean.getId(), null);
		}
	}

	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			this.db.delete(Content.table_name, Content.id_ + "=" + id,
					null);
		}

	}

	@Override
	public List<Content> get(String[] ids) {
		List<Content> list = new ArrayList<Content>();
		for (String id : ids) {
			Cursor c = db.query(Content.table_name, cols, Content.id_ + "=?", new String[] { id }, null, null, Content.display_index+" asc");
			if (c.getCount() == 1) {
				c.moveToFirst();
				Content bean =parseBean(c);
				list.add(bean);
			}
		}
		return list;
	}

	String[] cols = new String[] { 
			Content.id_,
			Content.ch_content,
			Content.ch_name,
			Content.department_id,
			Content.display_index,
			Content.eng_content,
			Content.eng_name,
			Content.image_path
			};
	public List<Content> getByDepartment(String id) {
		 List<Content> list=new ArrayList<Content>();
		 Cursor c = db.query(Content.table_name, cols, Content.department_id + "=?", new String[] { id }, null, null, Content.display_index+" asc");
		 while(c.moveToNext()){
			 Content bean=parseBean(c);
			 list.add(bean);
		 }
		return list;
	}

	@Override
	public void deleteAll() {
		db.execSQL("delete from " + Content.table_name);

	}

	@Override
	Content parseBean(Cursor c) {
		Content bean = new Content();
		bean.setId(Integer.parseInt(c.getString(0)));
		bean.setChContent(c.getString(1));
		bean.setChName(c.getString(2));
		bean.setDisplayIndex(Integer.parseInt(c.getString(4)));
		bean.setEngContent(c.getString(5));
		bean.setEngName(c.getString(6));
		bean.setImagePath(c.getString(7));
		return bean;
	}

	@Override
	ContentValues splitBean(Content bean) {
		ContentValues cv = new ContentValues();

		cv.put(Content.id_, bean.getId());
		cv.put(Content.ch_content, bean.getChContent());
		cv.put(Content.ch_name, bean.getChName());
		cv.put(Content.department_id, bean.getDepartment().getId());
		cv.put(Content.display_index, bean.getDisplayIndex().toString());
		cv.put(Content.eng_content, bean.getEngContent());
		cv.put(Content.eng_name, bean.getEngName());
		cv.put(Content.image_path, bean.getImagePath());
		return cv;
	}

}
